package com.zlzlib.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlzlib.common.bean.PageDataDto;
import com.zlzlib.common.bean.PageDataVo;
import com.zlzlib.common.bean.ResultVo;
import com.zlzlib.common.util.SpringContextUtil;
import com.zlzlib.log.bean.enums.LogType;
import com.zlzlib.log.config.LogOperation;
import com.zlzlib.manager.bean.dto.ConditionChildDto;
import com.zlzlib.manager.service.BaseManagerService;
import com.zlzlib.mybatis.BasePo;
import com.zlzlib.vuePage.VuePageUtils;
import com.zlzlib.vuePage.bean.vo.FindPageVo;
import com.zlzlib.vuePage.config.FormSelectData;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.ParameterizedType;

/**
 * 查询页面控制器
 * PO==数据库对象 SER==服务层   FD==分页查询的条件
 * LV==分页查询数据返回对象
 *
 * @DateTime: 2024/5/22 16:00
 * @Author zlz
 * @Version 1.0
 */
@RequiredArgsConstructor
@Validated
public abstract class BaseFindController<PO extends BasePo, LV, FD extends PageDataDto,
        SER extends BaseManagerService<PO, LV, FD>> {

    protected Class<PO> poClazz = (Class<PO>) ((ParameterizedType)
            getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    protected Class<LV> lvClazz = (Class<LV>) ((ParameterizedType)
            getClass().getGenericSuperclass()).getActualTypeArguments()[1];
    protected Class<FD> fdClazz = (Class<FD>) ((ParameterizedType)
            getClass().getGenericSuperclass()).getActualTypeArguments()[2];

    protected final SER service;
    protected final ModelMapper modelMapper;

    @ApiOperation(value = "获得查询管理页面的列表配置和筛选条件配置")
    @LogOperation(type = LogType.operation)
    @GetMapping("/findPageItem")
    @ResponseBody
    public ResultVo findPageItem() {
        FindPageVo vo = new FindPageVo();
        vo.setTable(VuePageUtils.getTableItemList(lvClazz));
        vo.setFind(VuePageUtils.getFormItemList(fdClazz));
        return ResultVo.success(vo);
    }

    @ApiOperation(value = "分页查询")
    @LogOperation(type = LogType.operation)
    @GetMapping("/findPageByCondition")
    @ResponseBody
    public ResultVo findPageByCondition(@Valid FD dto) {
        Page<LV> page = service.findPageByCondition(dto);
        return ResultVo.success(new PageDataVo(page.getTotal(), page.getRecords()));
    }

    @ApiOperation(value = "获得联动子类的列表数据")
    @LogOperation(type = LogType.operation)
    @PostMapping("/findConditionChild")
    @ResponseBody
    public ResultVo findConditionChild(@RequestBody @Valid ConditionChildDto dto) {
        FormSelectData findSer;
        try {
            //通过查询类名获得对象
            findSer = (FormSelectData) SpringContextUtil.getBean(Class.forName(dto.getClassName()));
        } catch (Exception e) {
            return ResultVo.error("查询类加载失败!");
        }
        return ResultVo.success(findSer.findConditionListChild(dto.getKey()));
    }

}
